/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.DragonPokerServer.Rules;

import java.util.ArrayList;
import org.DragonPokerServer.Classes.Card;
import org.DragonPokerServer.Classes.HandPlayer;
import org.DragonPokerServer.Classes.Points;
import org.DragonPokerServer.Thread.ThreadUser;

/**
 *
 * @author nykola
 */
public class StraightFlushRule extends GenericRule {

     @Override
    public int processRequest(ArrayList<Card> cards, HandPlayer hp) {
                 System.out.println("Rules: StraightFlushRule");
                 hp.setPoints(7);
                 return 0;
    }

    @Override
    public boolean isToProcessing(ArrayList<Card> cards, HandPlayer hp) {
        if (checkCards(cards, hp.getCards()) == true) {
            return true;
        }
        else
            setNext(new PokerRule());
        return false;
    }

    public boolean checkCards(ArrayList<Card> tablecards, ArrayList<Card> playercards){
        ArrayList<Card> cards = new ArrayList<Card>();
        cards.add(playercards.get(0));
        cards.add(playercards.get(1));
        for (Card a : tablecards) {
            cards.add(a);
        }
        //aggiungere controllo delle carte se sono in fila
        //e se sono dello stesso "pint"
        for (int i = 0; i < cards.size() - 1; i++) {
            for (int z = 0; z < cards.size() - 1; z++) {
                Card temp;
                if (cards.get(z).getValue() > cards.get(z + 1).getValue()) {
                    temp = cards.get(z + 1);
                    cards.add(z + 1, cards.get(z));
                    cards.remove(z + 2);
                    cards.add(z, temp);
                    cards.remove(z + 1);
                }
            }
        }

        int conta = 1;
        Card c = null;
        for(int i = 0; i < cards.size() - 1; i++){
            if((cards.get(i).getValue() + 1) == cards.get(i + 1).getValue() && cards.get(i).getSuit() == cards.get(i + 1).getSuit()){
                conta++;
                c = cards.get(i + 1);
            }
            else{
                if(conta < 5){
                    conta = 1;
                }
                else{
                    break;
                }
            }
        }

       if(conta >= 5 && c.getValue() != 1)
           return true;

       return false;
    }

   @Override
   public ArrayList getWinners(ArrayList<Card> communityCards, ArrayList<HandPlayer> playerCards) {
      throw new UnsupportedOperationException("Not supported yet.");
   }


}
